package com.qiche.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiche.aspect.Result;
import com.qiche.mapper.CategoryBannerMapper;
import com.qiche.model.CategoryBanner;
import com.qiche.service.CategoryBannerService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * User: ZHS
 * Date: 2018/7/12 11:39
 */
@Service("categoryBannerService")
public class CategoryBannerServiceImpl extends BaseService<CategoryBanner> implements CategoryBannerService {
    @Resource
    private CategoryBannerMapper mapper;

    @Override
    public PageInfo<CategoryBanner> selectAllByPage(int page, int limit) {
        Example example = new Example(CategoryBanner.class);
        example.setOrderByClause("sort desc");
        Example.Criteria criteria = example.createCriteria();
        PageHelper.startPage(page, limit);
        List<CategoryBanner> categoryBannerList = selectByExample(example);
        return new PageInfo<>(categoryBannerList);
    }

    @Override
    @Transactional
    public Integer saveBanner(CategoryBanner category) {
        mapper.insertResultId(category);//获取自增后的id
        category.setSort(category.getId());
        int count = mapper.updateByPrimaryKeySelective(category);
        return count;
    }

    @Transactional
    @Override
    public Result upOrDown(Long id, String type) {
        CategoryBanner banner = null;
        CategoryBanner categoryBanner = selectByKey(id);
        Example example = new Example(CategoryBanner.class);
        Example.Criteria criteria = example.createCriteria();
        if ("up".equals(type)){
            example.setOrderByClause("sort asc");
            criteria.andGreaterThan("sort",categoryBanner.getSort());
            List<CategoryBanner> categoryBannerList = selectByExample(example);
            if (categoryBannerList!= null && categoryBannerList.size()==0){
                return Result.createBySuccess("已置顶");
            }else{
                banner = categoryBannerList.get(0);
                int sort = banner.getSort();
                banner.setSort(categoryBanner.getSort());
                categoryBanner.setSort(sort);
                updateAll(banner);
                updateAll(categoryBanner);
                return Result.createBySuccess("移动成功");
            }
        }else if ("down".equals(type)){
            example.setOrderByClause("sort desc");
            criteria.andLessThan("sort",categoryBanner.getSort());
            List<CategoryBanner> categoryBannerList = selectByExample(example);
            if (categoryBannerList!= null && categoryBannerList.size()==0){
                return Result.createBySuccess("已到底");
            }else{
                banner = categoryBannerList.get(0);
                int sort = banner.getSort();
                banner.setSort(categoryBanner.getSort());
                categoryBanner.setSort(sort);
            }
            updateAll(banner);
            updateAll(categoryBanner);
            return Result.createBySuccess("移动成功");
        }

        return Result.createByErrorMessage("移动失败");
    }
    
    @Override
	public Result<?> selectAll() throws Exception{
		Example example = new Example(CategoryBanner.class);
        example.setOrderByClause("sort desc");
        Example.Criteria criteria = example.createCriteria();
        List<CategoryBanner> categoryBannerList = selectByExample(example);
		return Result.createBySuccess(categoryBannerList);
	}
}
